﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace WeManageService
{
    public static class DelayRecorder//共三次机会，每次机会充能时间为一周
    {
        public static bool State { get; set; }//true表示可推迟
        public static List<DateTime> NextTime { get; set; }//下次补充时间
        public static int Count = 3;

        private static Timer timer = null;


        public static void Delay()
        {
            lock(NextTime)
            {
                for (int i = 0;i < 3;i++)
                {
                    if (NextTime[i]<DateTime.Now)
                    {
                        NextTime[i] = DateTime.Now.AddDays(7);
                        break;
                    }
                }
            }
        }

        public static void StartTimer()
        {
            TimerState state = new TimerState();
            timer = new Timer(CheckTime, state, 0, 1000);
        }

        public static void StopTimer()
        {
            timer.Dispose();
        }

        public static void CheckTime(object state)
        {
            lock (NextTime)
            {
                Count = 3;
                for (int i = 0; i < 3; i++)
                {
                    if (NextTime[i] > DateTime.Now)
                    {
                        Count--;
                    }
                }
                if (Count == 0) State = false;
                else State = true;
            }
        }

        public static void GetRecord()//从xml文件中读上次记录
        {
            NextTime = FileManager.NextTime;
            //此处要判断当前是否可以推迟（是否上次推迟超过一周
        }

        public static void WriteRecord()//调用FileManager写入xml
        {
            lock(NextTime)
            {
                FileManager.NextTime = NextTime;
            }
        }
    }
}
